﻿using static System.Net.Mime.MediaTypeNames;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace GXYLinqDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            List<ScanPoint> points = new List<ScanPoint>();
            points.Add(new ScanPoint { Id = 1, RecipeId = 1, Ordinal = 1, PatternId = 1 });
            points.Add(new ScanPoint { Id = 2, RecipeId = 1, Ordinal = 2, PatternId = 1 });
            points.Add(new ScanPoint { Id = 3, RecipeId = 1, Ordinal = 3, PatternId = 1 });
            points.Add(new ScanPoint { Id = 4, RecipeId = 1, Ordinal = 4, PatternId = 1 });
            points.Add(new ScanPoint { Id = 5, RecipeId = 1, Ordinal = 5, PatternId = 1 });
            points.Add(new ScanPoint { Id = 1, RecipeId = 1, Ordinal = 1, PatternId = 2 });
            points.Add(new ScanPoint { Id = 2, RecipeId = 1, Ordinal = 2, PatternId = 2 });
            points.Add(new ScanPoint { Id = 3, RecipeId = 1, Ordinal = 3, PatternId = 2 });
            points.Add(new ScanPoint { Id = 4, RecipeId = 1, Ordinal = 4, PatternId = 2 });

            List<Pattern> patterns = new List<Pattern>();
            patterns.Add(new Pattern { Id = 1, Kind = 2, Ordinal = 1, Dx = 0.1, Dy = 0.2, Names = new List<string> { "OM1", "OM2" } });
            patterns.Add(new Pattern { Id = 2, Kind = 2, Ordinal = 2, Dx = 0.3, Dy = 0.4, Names = new List<string> { "SEM1", "SEM2" } });

            var result = points.Where(s => s.RecipeId == 1 && s.Ordinal > 0).Join(patterns, loc => loc.PatternId, p => p.Id, (loc, p) => new
            {
                Location = loc,
                Pattern = p
            }).OrderBy(e => e.Location.Ordinal).Select(e => new
            {
                Id = e.Location.Id,
                Ordinal = e.Location.Ordinal,
               eee = e.Pattern.Dx,
                fff = e.Pattern.Dy,
                PatternId = e.Pattern.Id,
                PatternOrdinal = e.Pattern.Ordinal,
                Name = string.Join(".", e.Pattern.Names)
            }).ToList();

            //var points = await ctx.ScanPoints
            //    .Where(s => s.RecipeId == id && s.Ordinal > 0)
            //    .Join(ctx.Pattegns, loc => loc.PatternId, p => p.Id, (loc, p) => new
            //    {
            //        Location = loc,
            //        Pattern = p
            //    })
            //    .OrderBy(e => e.Location.Ordinal)
            //    .Select(e => new RecipeScanPoint
            //    {
            //        Id = e.Location.Id,
            //        Ordinal = e.Location.Ordinal,
            //        X = e.Location.X,
            //        Y = e.Location.Y,
            //        Dx = e.Location.Dx,
            //        Dy = e.Location.Dy,
            //        PatternId = e.Pattern.Id,
            //        PatternOrdinal = e.Pattern.Ordinal,
            //        HasParent = e.Pattern.UseDifferentialDrive,
            //        ParentId = e.Pattern.ParentId ?? 0,
            //        Name = string.Join(".", e.Pattern.Names)
            //    })
            //    .ToListAsync()
            //    .ConfigureAwait(false);
            //            return points;

        }
    }

    [Table(nameof(Pattern))]
    public class Pattern
    {
        static readonly JsonSerializerOptions JsonOptions = new()
        {
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
            IncludeFields = true
        };

        public int Id { get; set; }

        /// <summary>
        /// 0:OM-光学显微镜,1:SEM-扫描电子显微镜,2:MP
        /// </summary>
        public int Kind { set; get; }

        public int Ordinal { get; set; }

        public double Dx { get; set; }

        public double Dy { get; set; }

        public double AddrOffsetX { get; set; }

        public double AddrOffsetY { get; set; }

        [NotMapped]
        public string ReferenceImagePath { get; set; }
        [NotMapped]
        public string AddressingImagePath { get; set; }

        public List<string> Names { get; set; } = [];

        public bool UseAddressing { get; set; }

        public int RefImageId { get; set; }

        public int? AddressingImageId { get; set; }

        public List<ScanPoint> ScanPoints { get; set; } = [];
    }

    // 假设的 ScanPoint 类
    public class ScanPoint
    {
        public int Id { get; set; }
        public int RecipeId { get; set; }
        public int Ordinal { get; set; }
        public int PatternId { get; set; }
    }
}
